package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.TransportApiApplyRpcService;
import com.hunttown.mes.rpc.domain.TransportApiApplyDTO;
import com.hunttown.mes.rpc.domain.query.TransportApiApplyDTOQuery;
import com.hunttown.mes.service.TransportApiApplyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.TransportApiApplyConverts.convert;

/**
 * created by wangjunfu on 2019-07-04 19:01:06
 */
@Service
public class TransportApiApplyRpcServiceImpl implements TransportApiApplyRpcService {

    private final Logger logger = LoggerFactory.getLogger(TransportApiApplyRpcServiceImpl.class);

    private final TransportApiApplyService transportApiApplyService;

    @Autowired
    public TransportApiApplyRpcServiceImpl(TransportApiApplyService transportApiApplyService) {
        this.transportApiApplyService = transportApiApplyService;
    }

    @Override
    public TransportApiApplyDTO insert(TransportApiApplyDTO transportApiApplyDTO) {
        try {
            return convert(transportApiApplyService.insert(convert(transportApiApplyDTO)));
        } catch (Exception e) {
            logger.error("TransportApiApply create failed.", e);
            throw new RuntimeException("TransportApiApply create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(TransportApiApplyDTO transportApiApplyDTO) {
        try {
            return transportApiApplyService.updateInfoById(convert(transportApiApplyDTO));
        } catch (Exception e) {
            logger.error("TransportApiApply update by id failed.", e);
            throw new RuntimeException("TransportApiApply update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(TransportApiApplyDTOQuery transportApiApplyDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportApiApplyDTOQuery.getW_id() != null && transportApiApplyDTOQuery.getW_id() > 0) {
                query.put("w_id", transportApiApplyDTOQuery.getW_id());
            }

            if (transportApiApplyDTOQuery.getIds() != null && !transportApiApplyDTOQuery.getIds().equals("")) {
                query.put("ids", transportApiApplyDTOQuery.getIds());
            }

            if (transportApiApplyDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportApiApplyDTOQuery.getStopFlag());
            }

            if (transportApiApplyDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportApiApplyDTOQuery.getDeleteFlag());
            }

            return transportApiApplyService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("TransportApiApply update by query failed.", e);
            throw new RuntimeException("TransportApiApply update by query failed, Please order your code.", e);
        }
    }

    @Override
    public TransportApiApplyDTO selectById(Integer id) {
        try {
            return convert(transportApiApplyService.selectById(id));
        } catch (Exception e) {
            logger.error("TransportApiApply select by id failed.", e);
            throw new RuntimeException("TransportApiApply select by id failed, Please order your code.", e);
        }
    }

    @Override
    public TransportApiApplyDTO selectObjByQuery(TransportApiApplyDTOQuery transportApiApplyDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (StringUtils.isNotBlank(transportApiApplyDTOQuery.getApiName())) {
                query.put("apiName", transportApiApplyDTOQuery.getApiName());
            }
            if (StringUtils.isNotBlank(transportApiApplyDTOQuery.getToken())) {
                query.put("token", transportApiApplyDTOQuery.getToken());
            }
            if (transportApiApplyDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportApiApplyDTOQuery.getStopFlag());
            }
            return convert(transportApiApplyService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("TransportApiApply select obj by query failed.", e);
            throw new RuntimeException("TransportApiApply select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<TransportApiApplyDTO> getListForPage(TransportApiApplyDTOQuery transportApiApplyDTOQuery) {
        Query query = Query.page(transportApiApplyDTOQuery.getPn(), transportApiApplyDTOQuery.getPageSize());

        if (transportApiApplyDTOQuery.getId() != null && transportApiApplyDTOQuery.getId() > 0) {
            query.put("id", transportApiApplyDTOQuery.getId());
        }

        if (transportApiApplyDTOQuery.getIds() != null && !transportApiApplyDTOQuery.getIds().equals("")) {
            query.put("ids", transportApiApplyDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(transportApiApplyDTOQuery.getLikeName())) {
            query.put("likeName", transportApiApplyDTOQuery.getLikeName());
        }

        if (StringUtils.isNotBlank(transportApiApplyDTOQuery.getLikeApiName())) {
            query.put("likeApiName", transportApiApplyDTOQuery.getLikeApiName());
        }

        if (transportApiApplyDTOQuery.getClientType() != null && transportApiApplyDTOQuery.getClientType() != -9999) {
            query.put("clientType", transportApiApplyDTOQuery.getClientType());
        }

        if (StringUtils.isNotBlank(transportApiApplyDTOQuery.getShowDate())) {
            query.put("showDate", transportApiApplyDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(transportApiApplyDTOQuery.getStartDate())) {
            query.put("startDate", transportApiApplyDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(transportApiApplyDTOQuery.getEndDate())) {
            query.put("endDate", transportApiApplyDTOQuery.getEndDate());
        }

        if (StringUtils.isNotBlank(transportApiApplyDTOQuery.getOrderBy())) {
            query.put("orderBy", transportApiApplyDTOQuery.getOrderBy());
        }

        if (transportApiApplyDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", transportApiApplyDTOQuery.getStopFlag());
        }

        List<TransportApiApplyDTO> list = convert(transportApiApplyService.selectByQuery(query));
        Page<TransportApiApplyDTO> page = Page.from(transportApiApplyDTOQuery.getPn(), transportApiApplyDTOQuery.getPageSize(), list);
        page.setTotalItems(transportApiApplyService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return transportApiApplyService.deleteById(id);
        } catch (Exception e) {
            logger.error("TransportApiApply delete failed.", e);
            throw new RuntimeException("TransportApiApply delete failed, Please order your code.", e);
        }
    }

    @Override
    public void updateUseTimes(Integer id) {
        try {
            transportApiApplyService.updateUseTimes(id);
        } catch (Exception e) {
            logger.error("TransportApiApply update use_times failed.", e);
            throw new RuntimeException("TransportApiApply update use_times failed, Please order your code.", e);
        }
    }
}